/**
 * Created with JetBrains WebStorm.
 * User: a_polyakov
 * Date: 05.03.13
 * Time: 20:11
 * To change this template use File | Settings | File Templates.
 */
exports.Model = function () {
    this.text_list=[];
    this.text_map = {};
    this.country_list = [];
    this.country_map = {};
    this.link_map = {};

    this.addCountry = function (countryFileName, countryName) {
        var country = this.country_map[countryFileName];
        if (country == null) {
            country = new exports.Country(countryFileName, countryName);
            this.country_list.push(country);
            this.country_map[countryFileName] = country;
        }
        return country;
    }
    this.getCountry = function (countryFileName) {
        var country = this.country_map[countryFileName];
        return country;
    }
    this.sort = function () {
        for (var i = 0; i < this.country_list.length; i++) {
            this.country_list[i].sort();
        }
        return sort_by_file_name(this.country_list);
    }
//    this.getCountry = function (country_name) {
//        return find_by_file_name(this.country_list, country_name, 0, this.country_list.length-1);
//    }
}

exports.Country = function (file_name, name) {
    this.file_name = file_name;
    this.name = name;
    this.city_list = [];
    this.city_map = {};

    this.addCity = function (cityFileName, cityName) {
        var city = this.city_map[cityFileName];
        if (city == null) {
            city = new exports.City(cityFileName, cityName);
            this.city_list.push(city);
            this.city_map[cityFileName] = city;
        }
        return city;
    }
    this.getCity = function (cityFileName) {
        var city = this.city_map[cityFileName];
        return city;
    }
    this.sort = function () {
        for (var i = 0; i < this.city_list.length; i++) {
            this.city_list[i].sort();
        }
        return sort_by_file_name(this.city_list);
    }
//    this.getCity = function (city_name) {
//        return find_by_file_name(this.city_list, city_name, 0, this.city_list.length-1);
//    }
}

exports.City = function (file_name, name) {
    this.file_name = file_name;
    this.name = name;
    this.department_list = [];
    this.department_map = {};

    this.addDepartment = function (department) {
        if (this.department_map[department.file_name] != null) {
            var currentNum = 2;
            var index = department.file_name.lastIndexOf('_');
            if (index > 0) {
                var numStr = department.file_name.substring(index + 1);
                currentNum = Number(numStr);
                if (currentNum != null && !isNaN(currentNum)) {
                    department.file_name = department.file_name.substring(0, index);
                    currentNum++;
                }
                else
                    currentNum = 2;
            }
            department.file_name = department.file_name + '_' + currentNum;
            this.addDepartment(department);
        } else {
            this.department_list.push(department);
            this.department_map[department.file_name] = department;
        }
        return department;
    }
    this.getDepartment = function (departmentFileName) {
        var department = this.department_map[departmentFileName];
        return department;
    }
    this.sort = function () {
        return sort_by_file_name(this.department_list);
    }
//    this.getDepartment = function (department_name) {
//        return find_by_file_name(this.department_list, department_name, 0, this.department_list.length-1);
//    }
}

exports.Department = function (file_name, name, address, zip_code, flag1, phone) {
    this.file_name = file_name;
    this.name = name;
    this.address = address;
    this.zip_code = zip_code;
    this.flag1 = flag1;
    this.phone = phone;
    this.work_time_list = [];
    this.service_list = [];
}

exports.DepartmentWorkTime = function (day_of_the_week, time) {
    this.day_of_the_week = day_of_the_week;
    this.time = time;
}

exports.DepartmentService = function (name) {
    this.name = name;
}

function find_by_file_name(array, file_name, s, e) {
    if (s <= e) {
        if (file_name == array[s].file_name) {
            return array[s];
        } else if (file_name > array[s].file_name) {
            if (file_name == array[e].file_name) {
                return array[e];
            } else if (file_name < array[e].file_name) {
                var i = Math.floor((e + s) / 2);
                if (file_name == array[i].file_name) {
                    return array[i];
                }
                else if (file_name < array[i].file_name) {
                    return find_by_file_name(array, file_name, s, i - 1);
                } else {
                    return find_by_file_name(array, file_name, i + 1, e);
                }
            }
        }
    }
    return null;
}

function sort_by_file_name(array) {
    return array.sort(object_file_name_compare);
}

function object_file_name_compare(a, b) {
    return (a.file_name > b.file_name) ? 1 : (a.file_name < b.file_name) ? -1 : 0;
}

// extend
//ClassB.prototype = new ClassA();

